package com.example.demo.refactor;

import java.util.Enumeration;
import java.util.Vector;

/**
 * 顾客
 */
public class Customer {
    private String name;

    private Vector _rentals = new Vector();

    public Customer(String name) {
        this.name = name;
    }

    public void addRental(Rental rental) {
        _rentals.addElement(rental);
    }

    public String getName() {
        return name;
    }

    public String statement() {
        Enumeration rentals = _rentals.elements();
        String result = "";
        while (rentals.hasMoreElements()) {

            Rental each = (Rental) rentals.nextElement();

            //show figures for this rental
            result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";
        }

        //add footer lines
        result+="Amount owed is "+ String.valueOf(getTotalAmount())+"\n";

        result+="You earned "+String.valueOf(getFrequentRenterPoints())+ "frequent renter points";


        return result;
    }

    private double getTotalAmount() {
        Enumeration rentals = _rentals.elements();
        double totalAmount = 0;//总金额
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            //show figures for this rental
            totalAmount += each.getCharge();
        }

        return totalAmount;
    }

    private int getFrequentRenterPoints(){
        int frequentRenterPoints = 0;//本次总积分
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            // add frequent renter points
            frequentRenterPoints+= each.getFrequentRenterPoints();
        }
        return frequentRenterPoints;
    }


    public Dd statement(Dd result){
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            result.transfrom((Rental)rentals);
        }
        return result;
    }

    private class Dd {
        private double totalAmount;
        private int frequentRenterPoints;

        public double getTotalAmount() {
            return totalAmount;
        }

        public int getFrequentRenterPoints() {
            return frequentRenterPoints;
        }

        public Dd transfrom (Rental rental) {
            //show figures for this rental
            totalAmount += rental.getCharge();
            // add frequent renter points
            frequentRenterPoints+= rental.getFrequentRenterPoints();
            return this;
        }
    }
}
